package cn.jichangxiu.ucenter.service.impl;

import cn.jichangxiu.common.ucenter.entity.Authority;
import cn.jichangxiu.common.ucenter.entity.Role;
import cn.jichangxiu.common.ucenter.entity.User;
import cn.jichangxiu.common.utils.ListUtils;
import cn.jichangxiu.ucenter.mapper.AuthorityMapper;
import cn.jichangxiu.ucenter.mapper.RoleMapper;
import cn.jichangxiu.ucenter.mapper.UserMapper;
import cn.jichangxiu.ucenter.service.inte.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AuthorityMapper authorityMapper;
    @Autowired
    private RoleMapper roleMapper;

    public User getUserByUserId(String userId) {
        User user = null;
        try {
            user = userMapper.findById(userId).get();
        }catch (Exception e){

        }
        return user;
    }

    @Override
    public User getUserByAccount(String account) {
        User user = null;
        try {
            user = userMapper.findUserByAccount(account);
        } catch (Exception e) {

        }
        return user;
    }

    @Override
    public List<Authority> getUserAuths(User user) {
        List<Authority> auths = new ArrayList<>();
        if (user == null) {
            return auths;
        }
        List<Role> roles = new ArrayList<>();
        try {
            roles = roleMapper.findRolesByUserId(user.getId());
        }catch (Exception e){

        }
        if (roles.size() > 0) {
            List<String> rolePIds = new ArrayList<>();
            roles.forEach(r -> {
                if (r.getParentId() != null) {
                    rolePIds.add(r.getParentId());
                }
            });
            if (rolePIds.size() > 0) {
                List<Role> proles = new ArrayList<>();
                try {
                    proles = roleMapper.findRolesByRoleIds(rolePIds);
                }catch (Exception e){

                }
                if (proles.size() > 0){
                    roles.addAll(proles);
                    List<Role> newRoles = ListUtils.duremo(roles);
                    roles = newRoles;
                }
            }
            if (roles.size() > 0){
                List<String> rids = new ArrayList<>();
                roles.forEach(r -> {
                    rids.add(r.getId());
                });
                if (rids.size() > 0) {
                    List<Authority> as = new ArrayList<>();
                    try {
                        as = authorityMapper.findAuthoritiesByRoleIdMany(rids);
                    }catch (Exception e){

                    }
                    if (as.size() > 0){
                        auths.addAll(as);
                    }
                }
                if (auths.size() > 0) {
                    List<String> pauthIds = new ArrayList<>();
                    auths.forEach(r -> {
                        pauthIds.add(r.getParentId());
                    });
                    if (pauthIds.size() > 0){
                        List<Authority> ass = new ArrayList<>();
                        ass = authorityMapper.findAuthoritiesByAuthIds(pauthIds);
                        auths.addAll(ass);
                    }
                }
            }
        }
        List<Authority> nauths = ListUtils.duremo(auths);
        return nauths;
    }
}
